﻿<?php

class Application_Model_Carte {
	
	public $id;
	public $actif;

	public function Application_Model_Carte($id = '',$actif=false)
	{
		$this->id = $id;
	}

	public function getId()
	{
		return $this->id;
	}

	public function getActif()
	{
		return $this->actif;
	}

	public function GetNextId()
	{
		$table = new Application_Model_CartesTable();
		$max = $table->fetchOne($table->select()->from($table->_name,'MAX(id_carte)'));
		return $max;
	}

	public function getById($id)
	{
		$table = new Application_Model_CartesTable();
		$rows=$table->fetchAll($table->select()->where('id_carte = ?',$id));
		if(count($rows)>0)
		{
			$this->id=$id;
			$this->actif=$rows[0]['actif'];
		}
		else
		{
			$this->id=0;
		}
		return $this;
	}

	public function BloquerCarte()
	{
		$cartes=new Application_Model_CartesTable();
		$data = array('actif' => 0);
		$where = $cartes->getAdapter()->quoteInto('id_carte= ?', $this->id);
		$cartes->update($data, $where);
		$this->actif=false;
		//historique système
		$histo = new Application_Model_HistoSystemElement('Blocage de la carte numero '.$this->id);
		$histo->AddToHistoSystem();
		//fin historique
		return $this;
	}

	public function DebloquerCarte()
	{
		$cartes=new Application_Model_CartesTable();
		$where= array('id_carte = ?' => $this->id);
		$data = array('actif' => true);
		$cartes->update($data,$where);
		$this->actif=true;
		//historique système
		$histo = new Application_Model_HistoSystemElement('Déblocage de la carte numero '.$this->id);
		$histo->AddToHistoSystem();
		//fin historique
		return $this;
	}

	public static function getBloquees()
	{
		$cartes =new Application_Model_CartesTable();
		$where = array('actif'=>0);
		$res=array();
		$rows=$cartes->fetchAll($cartes->select()->where('actif = ?','FALSE'));
		foreach($rows as $c)
		{
			$res[]=new self($c['id_carte'],$c['actif']);
		}
		return $res;
	}
}